<template>
  <view class="category">
    <PageSearch @click="handleClick"></PageSearch>
  </view>
  <view class="sort">
    <view class="kinds" @click="show = true">
      <image class="img" src="@/static/tabBar/category.png"></image>
    </view>
    <u-scroll-list :indicator="false">
      <view
        v-for="item in list"
        :key="item.id"
        class="item"
        :class="currentList === item.id && 'active'"
        @click="() => handleChange(item.id)"
      >
        <view class="img">
          <image class="icon" :src="item.categoryImage"></image>
        </view>
        <view class="text u-line-1">{{ item.categoryName }}</view>
      </view>
    </u-scroll-list>
  </view>
  <view class="box" :style="listHeight">
    <treeSelect
      ref="treeSelectRef"
      :height="listHeight.height"
      @edit="handleEdit"
      :isShowShopData="false"
      :leftData="listTwo"
      :selectType="selectType"
      :rightData="getRightData"
    ></treeSelect>
  </view>
  <u-popup :show="show" mode="top" @close="show = false">
    <view>
      <view class="category">
        <view class="sortPopup">
          <view
            v-for="item in list"
            :key="item.id"
            class="item"
            :class="currentList === item.id && 'active'"
            @click="() => handleChange(item.id)"
          >
            <view class="img">
              <image class="icon" :src="item.categoryImage"></image>
            </view>
            <view class="text u-line-1">{{ item.categoryName }}</view>
          </view>
        </view>
      </view>
    </view>
  </u-popup>
  <EditOrder :options="options" ref="editRef" @submit="handleSubmit" />
</template>

<script setup>
import PageSearch from "@/components/search/pageSearch.vue";
import EditOrder from "@/components/commodity/editOrder.vue";
import { ref, computed } from "vue";
import { message } from "@/hooks/useToast.js";
import { useOrderStore } from "@/store/order";
import { useListHeight } from "@/hooks/useListHeight.js";
import { getCategoryList, getCategoryPage } from "@/api/order.js";
import { onShow, onLoad } from "@dcloudio/uni-app";
import Options from "./config";

const selectType = ref("");

onLoad((params) => {
  selectType.value = params.type;
});
const options = computed(() => {
  try {
    if (selectType.value) {
      return Options[selectType.value].options();
    }
    return [];
  } catch (e) {
    return [];
  }
});
onShow(() => {
  getMenuOne();
});
const { listHeight } = useListHeight({
  el: ".box",
});

// 一级菜单
const list = ref([]);
const listTwo = computed(() => {
  try {
    return list.value.filter((btn) => btn.id === currentList.value)[0].child;
  } catch (e) {
    console.log(e);
    return [];
  }
});
// 当前选中的一级菜单
const currentList = ref("");
const getMenuOne = async () => {
  const res = await getCategoryList();
  list.value = res;
  if (res[0]) {
    currentList.value = res[0].id;
  }
};
const handleChange = (index) => {
  currentList.value = index;
};
const getRightData = async (params) => {
  const res = await getCategoryPage({
    page: params.page,
    limit: params.limit,
    categoryId: params.id,
  });
  return res;
};
const handleClick = () => {
  uni.navigateTo({
    url: "/pages/searchList/index",
  });
};
const show = ref(false);

const editRef = ref();
const handleEdit = (data) => {
  if (orderStore.hasShopItem(data)) {
    message("列表已经存在该单位商品,无法再次添加");
    return;
  }
  if (selectType.value === "Count") {
    // 盘点单
    data.costPrice = data.unitPrice;
    data.billQuantity = data.unitPrice;
  }
  editRef.value?.open(data);
};
const orderStore = useOrderStore();
const handleSubmit = (item) => {
  orderStore.setShopList({
    data: item,
    type: selectType.value,
  });
  uni.navigateBack();
};
</script>

<style lang="scss">
.sort {
  padding: 2px 12px 2px;
  position: relative;

  .kinds {
    position: absolute;
    right: 15px;
    top: 0;
    height: 43px;
    width: 24px;
    box-shadow: -10px 0px 10px #ccc;
    background-color: #fff;
    z-index: 1000;
    border-radius: 4px;

    .img {
      display: block;
      width: 12px;
      height: 12px;
      margin-top: 14px;
      margin-left: 5px;
    }
  }
}

.item {
  min-width: 66px;

  .img {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    overflow: hidden;
    border: 1px solid transparent;
    box-sizing: border-box;
    margin: 0 auto 15px;

    .icon {
      display: block;
      width: 100%;
      height: 100%;
    }
  }

  .text {
    color: #333333;
    font-size: 13px;
    text-align: center;
    width: 66px;
  }

  &.active {
    .img {
      border-color: #ff6105;
    }

    .text {
      font-weight: 500;
      color: #ff6105;
    }
  }
}

.sortPopup {
  display: flex;
  flex-wrap: wrap;
  padding: 2px 15px 28px;
  max-height: 300px;
  overflow-y: auto;

  .item {
    margin-bottom: 10px;
    .img {
      margin: 0 auto 15px;
    }

    .text {
      text-align: center;
    }
  }
}
</style>
